# -*- coding: utf-8 -*-
# @Time    : 2024/1/9 21:27
# @Author  : micah
# @File    : 12.jsonpath练习.py
# @Software: PyCharm


from jsonpath import jsonpath

info = {
    "store": {
        "book": [
            {"category": "reference",
             "author": "Nigel Rees",
             "title": "Sayings of the Century",
             "price": 8.95
             },
            {"category": "fiction",
             "author": "Evelyn Waugh",
             "title": "Sword of Honour",
             "price": 12.99
             },
            {"category": "fiction",
             "author": "Herman Melville",
             "title": "Moby Dick",
             "isbn": "0-553-21311-3",
             "price": 8.99
             },
            {"category": "fiction",
             "author": "J. R. R. Tolkien",
             "title": "The Lord of the Rings",
             "isbn": "0-395-19395-8",
             "price": 22.99
             }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}


# 1.获取第一本书的title
result = jsonpath(info, '$.store.book[0].title')
print(result)
result = jsonpath(info, "$['store']['book'][0]['title']")  # 外层必须是双引号, 内层为单引号
print(result)
result = jsonpath(info, '$..title')[0]
print(result)

# 2.提取2、3、4本书中的title
result = jsonpath(info, '$.store.book[1,2,3].title')
print(result)
result = jsonpath(info, "$.store.book[1,2,3]['title']")
print(result)
result = jsonpath(info, "$.store.book[1:4].title")
print(result)

# 3.提取第一本书与第三本书的title
result = jsonpath(info, "$.store.book[::2].title")
print(result)

# 4.提取最后一本书的title
result = jsonpath(info, '$.store.book[(@.length-1)].title')
print(result)
result = jsonpath(info, '$.store.book[-1:].title')
print(result)

# 5.提取价格小于10的书籍title
result = jsonpath(info, '$.store.book[?(@.price < 10)].title')
print(result)

# 6.提取价格小于或者等于20的所有书籍的price
result = jsonpath(info, '$..*[?(@.price <= 20)].price')
print(result)

# 7.获取所有书籍的作者
result = jsonpath(info, '$..author')
print(result)
result = jsonpath(info, '$.store.book[::].author')
print(result)
result = jsonpath(info, '$.store.book[*].author')
print(result)

# 8.获取商店中的自行车的所有信息
result = jsonpath(info, '$.store.bicycle')
print(result)

# 9.获取商店中所有商品的价格
result = jsonpath(info, '$.store..price')
print(result)

# 10.获取带有isbn信息的所有书籍
result = jsonpath(info, '$..book[?(@.isbn)].title')
print(result)

# 11.获取不带有isbn信息的所有书籍
result = jsonpath(info, '$..book[?(!@.isbn)].title')
print(result)

# 12.获取价格在5 - 10之间的书籍title: &&表示的是并且
result = jsonpath(info, '$..book[?(@.price > 5 && @.price < 10)].price')  # 并且
print(result)
result = jsonpath(info, '$..book[?(@.price < 5 || @.price > 10)].price')  # 或者
print(result)


# 13.获取所有元素
result = jsonpath(info, '$..')
print(result)
result = jsonpath(info, '$..*')
print(result)












